package cn.geoportal.benoserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.geoportal.benoserver.entity.IotType;
import cn.geoportal.benoserver.service.IotTypeService;
import cn.geoportal.benoserver.mapper.IotTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class IotTypeServiceImpl extends ServiceImpl<IotTypeMapper, IotType> implements IotTypeService{

    @Autowired
    private IotTypeMapper iotTypeMapper;

    /**
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public IPage<IotType> selectPage(Page page, Wrapper<IotType> wrapper) {
        return iotTypeMapper.selectPage(page, wrapper);
    }

    /**
     *
     * @param iotType
     * @return
     */
    @Override
    public int addIotType(IotType iotType) {
        QueryWrapper<IotType> wrapper = new QueryWrapper<>();
        wrapper.eq("name", iotType.getName());
        if(iotTypeMapper.selectCount(wrapper)>0){
            return 0;
        }
        return iotTypeMapper.insert(iotType);
    }

    /**
     *
     * @param name
     * @param description
     * @return
     */
    @Override
    public int addIotType(String name, String description) {
        QueryWrapper<IotType> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        if(iotTypeMapper.selectCount(wrapper)>0){
            return 0;
        }
        return iotTypeMapper.addIotType(name, description);
    }

    /**
     *
     * @param iotType
     * @return
     */
    @Override
    public int updateIotTypeById(IotType iotType) {
        QueryWrapper<IotType> wrapper = new QueryWrapper<>();
        wrapper.ne("id", iotType.getId()).eq("name", iotType.getName());
        if(iotTypeMapper.selectCount(wrapper)>0){
            return -1;
        }
        return iotTypeMapper.updateById(iotType);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public IotType findIotTypeById(Integer id) {
        return iotTypeMapper.selectById(id);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int deleteIotTypeById(Integer id) {
        return iotTypeMapper.deleteById(id);
    }
}




